﻿
using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Workshops;
using KDS.Mes.Mes.Workshops.Dtos;
using KDS.Mes.Web.Controllers;
using KDS.Mes.Web.Models.Workshop;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class WorkshopsController : MesControllerBase
    {

        private readonly IWorkshopAppService _workshopAppService;

        public WorkshopsController(IWorkshopAppService workshopAppService)
        {
            _workshopAppService = workshopAppService;

        }

        [HttpGet]
        [DontWrapResult]
        public JsonResult GetAllWorkshopByFacoryId(int id)
        {
            List<ComboboxItemDto> lst = new List<ComboboxItemDto>();
            NullableIdDto<int> input = new NullableIdDto<int>();
            input.Id = id;
            lst = _workshopAppService.GetWorkshopByFactoryId(input);
            return Json(lst, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            var model = new GetWorkshopInput { FilterText = Request.QueryString["filterText"] };
            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllWorkshops(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetWorkshopInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _workshopAppService.GetPagedWorkshops(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }




        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Basic_Workshops_Create, AppPermissions.Pages_Basic_Workshops_Edit)]
        public async Task<PartialViewResult> CreateOrEditWorkshopModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _workshopAppService.GetWorkshopForEditAsync(input);

            var viewModel = new CreateOrEditWorkshopModalViewModel(output);


            return PartialView("_CreateOrEditWorkshopModal", viewModel);
        }


    }
}